package com.example.springbootdemo.service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.stereotype.Service;

import com.example.springbootdemo.websocket.ProgressWebSocketHandler;

@Service
public class ProgressService {

    private final ProgressWebSocketHandler progressWebSocketHandler;
    private final ExecutorService executorService = Executors.newSingleThreadExecutor();

    public ProgressService(ProgressWebSocketHandler progressWebSocketHandler) {
        this.progressWebSocketHandler = progressWebSocketHandler;
    }

    /**
     * 启动进度模拟
     * 每秒更新一次进度，从1到100
     */
    public void startProgressSimulation() {
        executorService.submit(() -> {
            try {
                for (int progress = 1; progress <= 100; progress++) {
                    progressWebSocketHandler.broadcastProgressUpdate(progress);
                    Thread.sleep(1000); // 每秒更新一次
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
    }
}